//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.basic.action;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import java.io.File;
import java.io.IOException;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.bean.UploadConfigBean;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.config.MSProperties;
import org.apache.commons.lang3.StringUtils;

public abstract class BaseFileAction extends BaseAction {
    public BaseFileAction() {
    }

    public ResultData upload(UploadConfigBean config) throws IOException {
        String uploadMapping = MSProperties.upload.mapping;
        String uploadFileDenied = MSProperties.upload.denied;
        String uploadFolderPath = MSProperties.upload.path;
        String[] errorType = uploadFileDenied.split(",");
        String fileName = config.getFile().getOriginalFilename();
        if (StringUtils.isBlank(fileName)) {
            return ResultData.build().error("文件名不能为空!");
        } else {
            fileName = FileNameUtil.cleanInvalid(fileName);
            if (fileName.lastIndexOf(".") < 0) {
                this.LOG.info("文件格式错误:{}", fileName);
                return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("file.name")}));
            } else {
                String fileType = FileUtil.getSuffix(fileName);
                String realPath = config.isUploadFolderPath() ? BasicUtil.getRealPath("") : BasicUtil.getRealPath(uploadFolderPath);
                if (StringUtils.isNotBlank(config.getRootPath())) {
                    realPath = config.getRootPath();
                }

                String[] var9 = errorType;
                int var10 = errorType.length;

                String path;
                for(int var11 = 0; var11 < var10; ++var11) {
                    path = var9[var11];
                    if (fileType.equalsIgnoreCase(path)) {
                        this.LOG.info("文件类型被拒绝:{}", fileType);
                        return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("file.type")}));
                    }
                }

                if (config.isRename()) {
                    fileName = System.currentTimeMillis() + "." + fileType;
                }

                String uploadFolder = realPath + File.separator;
                if (StringUtils.isNotBlank(config.getUploadPath())) {
                    uploadFolder = uploadFolder + config.getUploadPath() + File.separator;
                }

                File saveFolder = new File(uploadFolder);
                File saveFile = new File(uploadFolder, fileName);
                if (!saveFolder.exists()) {
                    FileUtil.mkdir(saveFolder);
                }

                config.getFile().transferTo(saveFile);
                path = (config.isUploadFolderPath() ? "" : uploadMapping.replace("**", "")) + uploadFolder.replace(realPath, "") + "/" + fileName;
                return ResultData.build().success((new File("/" + path)).getPath().replace("\\", "/").replace("//", "/"));
            }
        }
    }

    public ResultData uploadTemplate(UploadConfigBean config) throws IOException {
        String uploadTemplatePath = MSProperties.upload.template;
        String uploadFileDenied = MSProperties.upload.denied;
        String[] errorType = uploadFileDenied.split(",");
        String fileName = config.getFile().getOriginalFilename();
        if (fileName.lastIndexOf(".") < 0) {
            this.LOG.info("文件格式错误:{}", fileName);
            return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("file.name")}));
        } else {
            String fileType = FileUtil.getSuffix(fileName);
            boolean isReal = (new File(uploadTemplatePath)).isAbsolute();
            String realPath = null;
            if (!isReal) {
                realPath = BasicUtil.getRealPath("");
            } else {
                realPath = uploadTemplatePath;
            }

            if (!config.isRename()) {
                fileName = config.getFile().getOriginalFilename();
                if (fileName.endsWith(".") && System.getProperty("os.name").startsWith("Windows")) {
                    this.LOG.info("文件类型被拒绝:{}", fileName);
                    return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("file.type")}));
                }

                fileType = FileUtil.getSuffix(fileName);
            } else {
                fileName = System.currentTimeMillis() + "." + fileType;
            }

            String[] var9 = errorType;
            int var10 = errorType.length;

            String path;
            for(int var11 = 0; var11 < var10; ++var11) {
                path = var9[var11];
                if (fileType.equalsIgnoreCase(path)) {
                    this.LOG.info("文件类型被拒绝:{}", fileType);
                    return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("file.type")}));
                }
            }

            String uploadFolder = realPath + File.separator;
            if (StringUtils.isNotBlank(config.getUploadPath())) {
                uploadFolder = uploadFolder + config.getUploadPath() + File.separator;
            }

            File saveFolder = new File(uploadFolder);
            File saveFile = new File(uploadFolder, fileName);
            if (!saveFolder.exists()) {
                FileUtil.mkdir(saveFolder);
            }

            config.getFile().transferTo(saveFile);
            path = uploadFolder.replace(realPath, "") + "/" + fileName;
            return ResultData.build().success((new File("/" + path)).getPath().replace("\\", "/").replace("//", "/"));
        }
    }
}
